//货物类别管理
//杨帆
interface IGoodsTypeManagentDetailViewModel {
    /**
     * 编号
     */
    id?: string;
    /**
     * 货物分类名称
     */
    name: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;
}

interface IGoodsTypeManagentViewModel extends IBaseViewModel<IGoodsTypeManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<GoodsTypeListItemResponse>;
    /**
     * 添加/编辑详情
     */
    detail: IGoodsTypeManagentDetailViewModel;
    /**
     * 添加货物类别
     */
    addGoodsType: () => void;
    /**
     * 编辑货物类别
     */
    editGoodsType: (id: string) => void;
    /**
     * 删除货物类别
     */
    deleteGoodsType: (id: string) => void;
    /**
     * 查询
     */
    query:()=>void;
    /**
     * 客户单位
     */
    clientName:string;
    /**
     * 列表高
     */
    heightA:number;
}

interface IGoodsTypeManagentQueryTerms {
    /**
     * 货物分类名称
     */
    name: string;
}

class GoodsTypeManagentController extends ListController<IGoodsTypeManagentViewModel, IGoodsTypeManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<GoodsTypeListItemResponse>;
    enterpriseId: string;
    

    constructor($scope: IGoodsTypeManagentViewModel, private goodsTypeService: IGoodsTypeService, private valueService: IValueService,
        private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.enterpriseId = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        this.$scope.clientName = JSON.parse(window.localStorage.getItem("client").toString()).clientName;

        this.$scope.addGoodsType = this.showAddGoodsTypeDialog;
        this.$scope.editGoodsType = this.showEditGoodsTypeDialog;
        this.$scope.deleteGoodsType = this.showDeleteGoodsTypeDialog;

        this.$scope.queryTerms = {
            name: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "分类名称", field: "name", enableColumnMenu: false },
                { name: "添加时间", field: "createTime", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#goodstype" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editGoodsType(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteGoodsType(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };

        this.query();
        this.$scope.query = this.query;

    }

    loadData(queryTerms: IGoodsTypeManagentQueryTerms, skip: number, count: number): void {
        this.goodsTypeService.getList(this.enterpriseId, queryTerms.name, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            this.$scope.gridOptions.data = result.data.data;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.heightA = (this.$scope.gridOptions.data.length *39)+95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    showDeleteGoodsTypeDialog = (id: string) => {
        this.$ngBootbox.confirm("确定删除此数据吗？").then(e => {
            this.goodsTypeService.delete(id).then(result => {
                this.reload();
            });
        });
    }

    showAddGoodsTypeDialog = () => {
        this.$scope.title = "添加货物类别";
        this.$scope.detail = {
            name: "",
            remarks: "",
            click: this.addGoodsType
        };
        $("#remarks").html("");
    }

    addGoodsType=()=> {
        if (!$("#goodsTypeForm").valid()) { return }
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.goodsTypeService.add(
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $("#goodstype").modal("hide");
            });
        });
    }

    showEditGoodsTypeDialog = (id: string) => {
        this.$scope.title = "编辑货物类别";
        var validator = $('#goodsTypeForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.goodsTypeService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                name: result.data.name,
                remarks: result.data.remarks,
                click: this.editGoodsType
            };
        });
    }

    editGoodsType=()=> {
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.goodsTypeService.edit(
                this.$scope.detail.id,
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $("#goodstype").modal("hide");
            });
        });
    }
}

angular.module("tms").controller("goodsTypeManagentController", ["$scope", "goodsTypeService", "valueService", "$ngBootbox", "$location",GoodsTypeManagentController]);